package activity;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.qzhy.female.R;

import Constant.Constance;
import adapter.CirclrPost;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import model.Articles;
import model.Category_Result;
import utils.GsonUtils;
import utils.HTTPUtils;
import utils.SetUtils;
import volley.VolleyListener;
import widget.MyListView;
import widget.MyListView.onRefreshListener;

public class CircleDetailActivity extends BaseActivity implements OnClickListener {

	private MyListView mPrListview;
	private CirclrPost postAdapter;
	private ImageView mImgPost;
	private List<Category_Result> mCategory_Result=new ArrayList<Category_Result>();
	private ImageView mImgBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_circle_detail);
		InitUI();
		GetDate();
	}
	private void GetDate() {
		showLoadingDialog();
		GetCategoryPost();
	}

	private void GetCategoryPost() {
		HTTPUtils.get(getApplicationContext(), Constance.URL.USERCIRCLE+"/"+SetUtils.GetCircleID(getApplicationContext())+"/articles", new VolleyListener() {

			@Override
			public void onResponse(String arg0) {
				Articles articles = GsonUtils.parseJSON(arg0, Articles.class);
				if(articles.getStatus().equals("success")){
					dismissLoadingDialog();
					mCategory_Result.clear();
					List<Category_Result> result = articles.getResult();
					mCategory_Result.addAll(result);
					postAdapter.notifyDataSetChanged();
//					Toast.makeText(getApplicationContext(), "文章数量："+mCategory_Result.size(), 1).show();
				}
			}
			@Override
			public void onErrorResponse(VolleyError arg0) {
				showLoadingDialog();
			}
		});
	}

	private void InitUI() {
		mImgPost = (ImageView) findViewById(R.id.img_post);
		mImgBack = (ImageView) findViewById(R.id.img_back);
		mImgBack.setOnClickListener(this);
		mImgPost.setOnClickListener(this);
		
		mPrListview = (MyListView) findViewById(R.id.ptrListView);
		postAdapter = new CirclrPost(this,mCategory_Result);
		mPrListview.setAdapter(postAdapter);
		mPrListview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(CircleDetailActivity.this, "点击了第："+position+"个", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(CircleDetailActivity.this,PostDetailActivity.class);
				intent.putExtra("AriticleId", mCategory_Result.get(position-1).getId());
				startActivity(intent);
			}
		});
		mPrListview.setOnRefreshListener(new onRefreshListener() {
			@Override
			public void refresh() {
				new GetDataTask().execute();
			}
			@Override
			public void loadMore() {
				mPrListview.onRefreshComplete();
			}
		});
	}
	private class GetDataTask extends AsyncTask<Void, Void, String> {
		protected String doInBackground(Void... params) {
			try {
				Thread.sleep(3000);
				GetCategoryPost();
			} catch (InterruptedException e) {
			}
			return "";
		}

		protected void onPostExecute(String result) {
//			mGoodList.addAll(0, result);
//			mAdapter.notifyDataSetChanged();
				// 关闭正在加载动画布局
			mPrListview.onRefreshComplete();
		}
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_post:
			UserPost();
			break;
		case R.id.img_back:
			finish();
			break;
		default:
			break;
		}
	}
	private void UserPost() {
		if(SetUtils.islogined(this)){
			Intent intent = new Intent(this,RepliesActivity.class);
			intent.putExtra(Constance.INTENT.REPLIES, 3);
			startActivityForResult(intent, 12312);
		}else {
			startActivity(new Intent(this,LoginActivity.class));
		}
	}
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if(arg2!=null){
			showLoadingDialog("恭喜您，发帖成功");
			GetCategoryPost();
		}
		super.onActivityResult(arg0, arg1, arg2);
	}
	
}
